# -*- python -*-
# ex: set syntax=python:

# Copyright 2017 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

from buildbot.schedulers.basic import SingleBranchScheduler

from master import gitiles_poller
from master import master_utils
from master import slaves_list
from master.factory import remote_run_factory

import config
import master_site_config

ActiveMaster = master_site_config.V8Chromium

# This is the dictionary that the buildmaster pays attention to. We also use
# a shorter alias to save typing.
c = BuildmasterConfig = {}
c['status'] = []


####### DATABASE

config.DatabaseSetup(c)


####### CHANGESOURCES

comparator = gitiles_poller.GitilesRevisionComparator()
c['change_source'] = [
  gitiles_poller.GitilesPoller(
      'https://chromium.googlesource.com/chromium/src',
      branches=['master'],
      pollInterval=20,
      comparator=comparator,
  )
]


####### FACTORIES

def m_remote_run(recipe, **kwargs):
  return remote_run_factory.RemoteRunFactory(
      active_master=ActiveMaster,
      repository='https://chromium.googlesource.com/chromium/tools/build.git',
      recipe=recipe,
      factory_properties={'path_config': 'kitchen'},
      **kwargs)


####### BUILDER SPECS

CATEGORY_FUTURE = '1Future'

scheduled_builders = [
  {
    'name': 'Linux - Future',
    'factory': m_remote_run('chromium'),
    'category': CATEGORY_FUTURE,
  },
  {
    'name': 'Linux - Future (dbg)',
    'factory': m_remote_run('chromium'),
    'category': CATEGORY_FUTURE,
  },
  {
    'name': 'Linux V8 API Stability',
    'factory': m_remote_run('chromium'),
    'category': CATEGORY_FUTURE,
  },
]

triggered_builders = [
]


####### SCHEDULERS

c['schedulers'] = [
  SingleBranchScheduler(
      name='v8_chromium',
      branch='master',
      treeStableTimer=60,
      builderNames=[builder['name'] for builder in scheduled_builders],
  ),
]


####### BUILDERS

c['builders'] = (
    scheduled_builders +
    triggered_builders
)

# Associate the slaves to the builders. The configuration is in slaves.cfg.
slaves = slaves_list.SlavesList('slaves.cfg', 'V8Chromium')
for builder in c['builders']:
  builder['slavenames'] = slaves.GetSlavesName(builder=builder['name'])


####### BUILDSLAVES

# The 'slaves' list defines the set of allowable buildslaves. List all the
# slaves registered to a builder. Remove dupes.
c['slaves'] = master_utils.AutoSetupSlaves(c['builders'],
                                           config.Master.GetBotPassword())

# Make sure everything works together.
master_utils.VerifySetup(c, slaves)


####### STATUS TARGETS

# Buildbot master url:
# Must come before AutoSetupMaster().
c['buildbotURL'] = ActiveMaster.buildbot_url

# Adds common status and tools to this master.
master_utils.AutoSetupMaster(c, ActiveMaster,
    public_html='../master.chromium/public_html',
    tagComparator=comparator,
    templates=['../master.client.v8/templates',
               '../master.chromium/templates'])

# Adjust the buildCaches to be 3x the number of slaves per builder.
c['autoBuildCacheRatio'] = 3


####### PROJECT IDENTITY

c['projectName'] = ActiveMaster.project_name
c['projectURL'] = config.Master.project_url
